package com.lg.erp.controller.jml;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.jml.TDepartment;
import com.lg.erp.entity.jml.TPost;
import com.lg.erp.service.jml.ITDepartmentService;
import com.lg.erp.service.jml.ITPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
public class TDepartmentController {

    @Autowired
    private ITDepartmentService departmentService;
    @Autowired
    private ITPostService postService;

    //查询所有的部门
    @GetMapping("/departments")
    public JsonResult getAllDepartment(){
        List<TDepartment> departments = departmentService.findAllDepartment();
        return new JsonResult(1,"ok",departments);
    }
    //通过id查询
    @GetMapping("/departments/{id}")
    public JsonResult getStaffByPage(@PathVariable String id){
        TDepartment tDepartment = departmentService.getById(id);
        tDepartment.setPosts(postService.selectAll(tDepartment.getId()));
        /* TDepartment tDepartment = departmentService.selectById(id);*/
        return new JsonResult(1,"ok",tDepartment);
    }
    //修改
    @PutMapping("/department")
    public JsonResult updateStaff(@RequestBody TDepartment department){
        /*TPost post = postService.getOne(new QueryWrapper<TPost>()
                .lambda()
                .eq(TPost::getDId, department.getPName())
        );*/
        departmentService.updateById(department);
        return new JsonResult(1,"ok",null);
    }
    //增加
    @PostMapping("/department")
    public JsonResult addStaff(@RequestBody TDepartment department){
        /*department.setId(UUID.randomUUID().toString().substring(0,6));*/
        TPost post = new TPost();
        post.setName(department.getPName());
        post.setDId(UUID.randomUUID().toString().substring(0,6));
        post.setId(UUID.randomUUID().toString().substring(0,6));
        department.setCId("1");
        postService.save(post);
        department.setId(post.getDId());
        departmentService.save(department);
        return new JsonResult(1,"ok",null);
    }

    //删除部门
    @DeleteMapping("/department/{id}")
    public JsonResult deleteStaff(@PathVariable String id){
        departmentService.removeById(id);
        return new JsonResult(1,"ok",null);
    }

    //查询所有下级部门
    @GetMapping("/department")
    public JsonResult selectAllDepartment(){
        List<TDepartment> departments = departmentService.selectAll();
        return new JsonResult(1,"ok",departments);
    }
}
